package com.gandong8.trace.web.controller;

import com.gandong8.trace.biz.service.UserService;
import com.gandong8.trace.dao.domain.generated.User;
import com.gandong8.trace.domain.common.QueryData;
import com.gandong8.trace.web.domain.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * CREATE TABLE `user` (
 *   `id` int(11) NOT NULL,
 *   `english_name` varchar(100) NOT NULL,
 *   `chinese_name` varchar(100) NOT NULL,
 *   `mobile` varchar(100) NOT NULL,
 *   `age` int(11) NOT NULL,
 *   `add_time` int(11) NOT NULL
 * ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 */
@Slf4j
@Controller
@RequestMapping(value = "/mysql", produces = "application/json;charset=UTF-8")
public class MysqlController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/user/add")
    @ResponseBody
    public Result userAdd(){
        User user = new User();
        user.setAddTime((int) System.currentTimeMillis());
        user.setAge(1);
        user.setChineseName("刘刘");
        user.setEnglishName("jack");
        user.setMobile("1231");
        userService.userAdd(user);
        return Result.success(user);
    }

    @RequestMapping(value = "/user/info")
    @ResponseBody
    public Result userInfo(Integer id){
        return Result.success(userService.getById(id));
    }


    @RequestMapping(value = "/user/search")
    @ResponseBody
    public Result userSearch(Integer page, Integer pageSize, User user){
        QueryData<User> queryData = new QueryData<>();
        queryData.setPage(page);
        queryData.setPageSize(pageSize);
        queryData.setSearchData(user);
        return Result.success(userService.userSearch(queryData));
    }
}
